<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@ page import="com.onlineauction.helpers.Constants" %>
<%@ page import="com.onlineauction.helpers.Constants.Category" %>
<%@ page import="com.onlineauction.entities.Auction" %>
<%@ page import="java.util.List" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.Date" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Manage Auctions</title>

<!-- The following Scripts and Stylesheets are for the Date Picker -->
	<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" />
	<link rel="stylesheet" href="style.css" />
	<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
	<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
	<script src="timepicker_addon.js"></script>
	<style media="screen" type="text/css">
		.ui-timepicker-div .ui-widget-header { margin-bottom: 8px; }
		.ui-timepicker-div dl { text-align: left; }
		.ui-timepicker-div dl dt { height: 25px; margin-bottom: -25px; }
		.ui-timepicker-div dl dd { margin: 0 10px 10px 65px; }
		.ui-timepicker-div td { font-size: 90%; }
		.ui-tpicker-grid-label { background: none; border: none; margin: 0; padding: 0; }
		.ui-timepicker-rtl{ direction: rtl; }
		.ui-timepicker-rtl dl { text-align: right; }
		.ui-timepicker-rtl dl dd { margin: 0 65px 10px 10px; }
	</style>
<!-- End of Scripts and Stylesheets are for the Date Picker -->

</head>
<jsp:useBean id="auctionBean" class="com.onlineauction.beans.AuctionBean" scope="page" />

<%
	// redirect to Home if session is not set (user is not logged in yet)
	String username = (String) session.getAttribute(Constants.USERNAME);
	if (username == null) {
		request.getRequestDispatcher("/Home").forward(request, response);
	}
	
	String itemIDString = null;
	try
	{
		itemIDString = (String) request.getParameter(Constants.ITEM_ID);
	}
	catch(Exception E)
	{	}
	
	long itemID=0;
	
	if(itemIDString != null)
		 itemID = Long.parseLong(itemIDString);
	
    if (itemID == 0) 
    {
    	response.sendRedirect("/home.jsp");
    	return;
    }
%>

<body>

	<div class="wrapper">
		<div id="Content">
			<div id="Header">
				<H2>Manage Auctions for the Item: <% out.print(auctionBean.getItemName(itemID)); %></H2>
				<div id="LogOutButtonDiv" align="left">
					<a href="/Home">Home</a>
					<a href="/manageAuction.jsp?<%=Constants.ITEM_ID%>=<%=itemID%>">Refresh</a>
					<a href="LogoutAccount">Logout</a>	
				</div>
			</div>
			<hr />
			<div id="MainContent">
				<H3>Add a new auction</H3>
				<form action="ManageAuction" method="post">					
					<table>

						<tr>
							<td align="right">Ask price:</td>
							<td align="left"><input type="text" name=<%=Constants.ASK_PRICE%> /></td>
						</tr>
						<!--  
						<tr>
							<td align="right">Start time:</td>
							<td align="left"><input type="text" name=<%=Constants.START_TIME%> id=<%=Constants.START_TIME%> /> </td>
							<td> <h6>leave this field blank if you want  <br> to start the auction right away</h6></td>
						</tr>
						-->
						<tr>
							<td align="right">End time:</td>
							<td align="left"><input type="text" name=<%=Constants.END_TIME%> id=<%=Constants.END_TIME%> /></td>
						</tr>	
						<tr>
							<td><input type="hidden" name=<%=Constants.ITEM_ID%> value=<%=auctionBean.getItemID()%> /></td>
							<td><input type="submit" value="Add Auction" /> <br>
					 	</tr>
					</table>
				</form>
				 <%
			        String message = (String)request.getAttribute(Constants.MESSAGE);
			        if (message != null && message.length() != 0) {
			        	out.println("Message: " + message);
			        }
  		  	  	%>
  		  	  	<br>
											 
			</div>
			
			
			<!-- BELOW SCRIPT TAG IS FOR THE TIMESTAMP PICKER -->
			<script>
				$('#<%=Constants.START_TIME%>').datetimepicker();
				$('#<%=Constants.END_TIME%>').datetimepicker();
			</script>
			
			<hr />
			<!-- end MainContent -->
			<div id="MainContent2">
			<H3>Active Auction</H3>
				<table>
					
					<%
						SimpleDateFormat dateFormatter = new SimpleDateFormat("MM/dd/yyyy HH:mm");
						List<Auction> auctions = auctionBean.getLiveAuctionsForItem(itemID);
						if (auctions.size() > 0)
						{
					%>
					<tr>
						<th>Asking Price</th>
						<th>Start Date</th>
						<th>End Date</th>
						<th>Actions</th>
					</tr>
					<%
							for (Auction auction :auctions ) {
					%>
					<tr>
						<td>$<%=auction.getAskPrice() %></td>
						<td><%=dateFormatter.format((Date)auction.getStartTime())%></td>
						<td><%=dateFormatter.format((Date)auction.getEndTime())%></td>
						<td>
							<form action="CancelAuction" method="post">
								<input type="hidden" name=<%=Constants.AUCTION_ID%> value=<%=auction.getAuctionID()%> />
								<input type="hidden" name=<%=Constants.ITEM_ID%> value=<%=itemID%> />
								<input type="submit" value="Cancel Auction" />
							</form>
							<form action="ViewAuction" method="post">
								<input type="hidden" name=<%=Constants.AUCTION_ID%> value=<%=auction.getAuctionID()%> />
								<input type="hidden" name=<%=Constants.ITEM_ID%> value=<%=itemID%> />
								<input type="submit" value="View Auction" />
							</form>
						</td>
					</tr>
				</table>
				<%
							}
						}
						else
						{
							out.println("none");
						}
					%>
			</div>
			<!-- end MainContent -->
			<div id="MainContent2">
			<H3>Expired/cancelled auctions</H3>
				<table>
					
					<%
						List<Auction> expAuctions = auctionBean.getExpiredAuctionsForItem(itemID);
						if (expAuctions.size() > 0)
						{
					%>
					<tr>
						<th>Asking Price</th>
						<th>Start Date</th>
						<th>End Date</th>
						<th>Actions</th>
					</tr>
					<%
							for (Auction auction : expAuctions) {
					%>
					<tr>
						<td>$<%=auction.getAskPrice() %></td>
						<td><%=dateFormatter.format((Date)auction.getStartTime())%></td>
						<td><%=dateFormatter.format((Date)auction.getEndTime())%></td>
						<td>
							<form action="ViewAuction" method="post">
								<input type="hidden" name=<%=Constants.AUCTION_ID%> value=<%=auction.getAuctionID()%> />
								<input type="hidden" name=<%=Constants.ITEM_ID%> value=<%=itemID%> />
								<input type="submit" value="View Auction" />
							</form>
						</td>
					</tr>
				<%
							}
						}
						else
						{
							out.println("none");
						}
				%>
				</table>
			</div>
		</div>
		<!-- end Content -->
	</div>
	<!-- end wrapper -->


</body>
</html>
